<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Admin\House;
use App\Admin\Intrusion;
use Illuminate\Foundation\Console\Presets\React;
use Illuminate\Support\Facades\Storage;

class HouseController extends Controller
{
    // 家庭成员列表展示
    public function lst(Request $request)
    {
        // 如果是GET请求，那么就返回视图
        if ($request->isMethod('GET')) {
            if ($request->Input('page')) {
                $data = House::get()->toArray();
                $count = House::count();
                $msg = "";
                $code = 0;
                // dd($data);
                foreach ($data as $key => $val) {
                    $data[$key]['member_images'] = Storage::url($data[$key]['member_images']);
                }
                // dd($data);

                // dd(compact('data', 'count', 'msg', 'code'));
                return (compact('data', 'count', 'msg', 'code'));
            } else return view('admin.house.lst');
            // 如果是POST请求，就返回家庭成员信息
        } else {
            //

        }
    }

    public function add(Request $request)
    {
        if ($request->isMethod('GET')) return view('admin.house.add');
        else {
            /*  
            array:4 [
                "member_name" => "拉拉"
                "member_role" => "嗯嗯"
                "imageurl" => "FaceImage/26d37fb288bf8233aaf586a133160f2b8f008bb7.jpg"
                "file" => null
            ]
            */
            $info = $request->input('add_data');
            $res = House::insert([
                'member_name' => $info['member_name'],
                'member_role' => $info['member_role'],
                'member_images' => $info['imageurl']
            ]);
            dd($res);
        }
    }

    // 这是入侵视频视图
    public function intrusion(Request $request)
    {
        //        如果请求方式为GET则返回视图
        if ($request->isMethod('get')) {
            Storage::disk('oss');
            // $data = Intrusion::get()->toArray();
            $data = Intrusion::get();
            // dd($data);
            foreach ($data as $key => $val) {
                // $fileurl = 'RaspiVideo/' . $val->videoname . '.mp4';
                $val->capture = Storage::url('RaspiVideo/' . $val->videoname . '.mp4');
            }
            // $mpl3 = Storage::get('RaspiVideo/cat.jpg');
            // Storage::directories($directory);
            // $mpl3 = Storage::url('RaspiVideo/face_detect.mp4');
            return view('admin.house.intrusion', compact('data'));
        } else {
        }
    }

    // 删除家庭人员的方法
    public function del(Request $request)
    {
    }

    // 处理文件上传的方法
    public function fileupload(Request $request)
    {

        // dd($request->file('file')->getClientOriginalName());
        if ($request->isMethod('post')) {
            if ($request->file()) {
                $filename = sha1(time() . $request->file('file')->getClientOriginalName());
                // dd($filename);
                $image_type = explode('.', $request->file('file')->getClientOriginalName());
                $last = $image_type[count($image_type) - 1];
                $filepath = 'FaceImage/' . $filename . '.' . $last;
                Storage::disk('oss')->put($filepath, file_get_contents($request->file('file')->path()));
                $result = [
                    'code' => 0,
                    "msg" => $filepath,
                ];
                return $result;
            } else {
                $result = [
                    'code' => 1,
                    "msg" => "Server has error"
                ];
                return $result;
            }
        }
    }
}
